package com.twu.biblioteca.business;

import com.twu.biblioteca.data.entity.Book;
import com.twu.biblioteca.data.entity.Movie;
import com.twu.biblioteca.data.entity.User;

import java.util.*;

public class LibService {

    public static String BOOK_RESERVED_INFO = "Thank You! Enjoy the book." ;
    public static String BOOK_RESERVE_FAILED_INFO = "Sorry we don't have that book yet.";
    public static String lOGIN_INFO = "please log in first!";
    public static String lOGIN_SUCCEEDED_INFO = "login successfully";
    public static String PASSWORD_INCORRECT_INFO = "the password is incorrect";
    public static String USER_NOT_EXIST = "the user is not exist";

    private List<Book> books;
    private Map<String, Book> booksMap;
    private List<User> users;
    private List<Movie> movies;

    private BookService bookService;
    private UserService userService;
    private MovieService movieService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setBookService(BookService bookService){
        this.bookService = bookService;
    }
    public void addBook(Book book){
        this.booksMap.put(book.getId(),book);
    }

    public LibService() {
        bookService = new BookService();
        books = bookService.getBooks();
        userService = new UserService();
        this.initBooksMap();
    }


    private void initBooksMap(){
        booksMap = new LinkedHashMap<String, Book>();

        for (Book book: books){
            booksMap.put(book.getId(), book);
        }
    }

    public boolean reserveBook(String id) {

        if(userService.getLoginUser() == null) {
            return false;
        }

        if (!booksMap.containsKey(id)){
            return false;
        }

        if (booksMap.get(id).isReserved()){
            return false;
        }

        booksMap.get(id).setReserved(true);
        return true;
    }

    public String viewBook() {
        StringBuffer resultStr = new StringBuffer();
        Iterator iterator = booksMap.entrySet().iterator();

        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = entry.getKey().toString();
            Book value = (Book) entry.getValue();
            resultStr.append(key + "\t" + value.getName() + "\n");
        }

        return resultStr.toString();
    }

    public String  login(String userName,String password) {
        String resultStr = "";
        users = userService.getUsers();

        for(User user:users) {

            if(user.getUserName().equals(userName)) {

                if(user.getPassword().equals(password)) {
                    userService.login(user);

                    resultStr = lOGIN_SUCCEEDED_INFO;
                }
                else {
                    resultStr = PASSWORD_INCORRECT_INFO;
                }
                return resultStr;
            }
        }
        resultStr = USER_NOT_EXIST;

        return resultStr;
    }

    public String viewMovie() {
        StringBuffer resultStr = new StringBuffer();
        movieService = new MovieService();
        movies = movieService.getMovies();

        for(Movie movie:movies) {
            resultStr.append(movie.getName()+"  "+movie.getDate()+"  "+movie.getDirector()+"  "+movie.getRating()+"\n");
        }

        return resultStr.toString();
    }

    public String viewDetailOfLoginUser() {
        StringBuffer resultStr=new StringBuffer();
        User loginUser = userService.getLoginUser();
        if (loginUser!=null) {

            resultStr.append("Name:  "+loginUser.getName()+"\n");
            resultStr.append("Email:  "+loginUser.getEmail()+"\n");
            resultStr.append("Phone Number: "+loginUser.getPhoneNumber()+"\n");

        }

        else {
            resultStr.append(lOGIN_INFO+"\n");
        }
        return resultStr.toString();
    }

}
